﻿namespace TextFiles
{
    using System;
    using System.Collections.Generic;
    using System.IO;

    class SortStringsAndSaveToFile
    {
        // 1.2.12.06. Write a program that reads a text file containing a list of strings, sorts them and
        // saves them to another text file. Example:
        // Ivan		     	George
        // Peter	-->		Ivan
        // Maria			Maria
        // George			Peter

        static void Main()
        {
            StreamReader sr = new StreamReader(@"../../input.txt");

            List<string> stringsForSort = new List<string>();

            using (sr)
            {
                string line = sr.ReadLine();
                while (line != null)
                {
                    stringsForSort.Add(line);
                    line = sr.ReadLine();
                }
            }

            //string[] stringsForSort = new string[] { "Ivan", "Peter", "Maria", "Ivan", "Georgia" };

            for (int i = 0; i < stringsForSort.Count; i++)
            {
                stringsForSort[i] = stringsForSort[i].ToLower();
            }


            for (int i = 0; i < stringsForSort.Count - 1; i++)
            {
                string currentLowest = stringsForSort[i];
                int indexOfLowest = i;

                for (int j = i + 1; j < stringsForSort.Count; j++)
                {
                    int shortest = currentLowest.Length;
                    if (stringsForSort[j].Length < shortest)
                    {
                        shortest = stringsForSort[j].Length;
                    }

                    for (int k = 0; k < shortest; k++)
                    {
                        if (currentLowest[k] == stringsForSort[j][k])
                        {
                            continue;
                        }
                        else
                        {
                            if (currentLowest[k] < stringsForSort[j][k])
                            {
                                break;
                            }
                            else
                            {
                                currentLowest = stringsForSort[j];
                                indexOfLowest = j;
                                break;
                            }
                        }
                    }
                }
                if (stringsForSort[i] == currentLowest)
                {
                    continue;
                }
                else
                {
                    string temp = stringsForSort[i];
                    stringsForSort[i] = currentLowest;
                    stringsForSort[indexOfLowest] = temp;
                }
            }


            for (int i = 0; i < stringsForSort.Count; i++)
            {
                List<string> tempy = new List<string>();
                tempy.Add((stringsForSort[i][0]).ToString().ToUpper());
                for (int j = 1; j < stringsForSort[i].Length; j++)
                {
                    tempy.Add((stringsForSort[i][j]).ToString());
                }

                stringsForSort[i] = String.Join("", tempy);
            }

            StreamWriter sw = new StreamWriter(@"../../output.txt");
            using (sw)
            {
                for (int i = 0; i < stringsForSort.Count; i++)
                {
                    sw.WriteLine(stringsForSort[i]);
                }
            }
        }
    }
}